package com.jinyou.chengfengshop.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.adapter.MessageAdapter;
import com.jinyou.chengfengshop.bean.MessageBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * 消息列表
 * Created by zhuzhiqiang on 2018/9/26.
 */

public class MessageListActivity extends AppCompatActivity {
    private RecyclerView message_rec;
    private MessageAdapter mAdapter;
    private String tocken;
    private List<MessageBean> mb;
    private TextView back_tv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);


        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        tocken = sh.getString("tocken","");

        message_rec = (RecyclerView) findViewById(R.id.message_rec);
        GridLayoutManager layoutManage = new GridLayoutManager(this, 1);
        message_rec.setLayoutManager(layoutManage);
        getMessage();
        back_tv = (TextView) findViewById(R.id.back_tv);
        back_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    //获取消息列表
    private void getMessage(){
        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("token",tocken);
        paramsMap.put("sysCustomer","o2o-chengfeng");
        OkhttpUtil.okHttpPost(AppUrl.MESSAGE_LIST, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(MessageListActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {

                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");
                    if(code == 1){
                        if(code == 1){
                            String ob = obj.getString("data");

                            if (ob != null && !ob.equals("")) {
                                Type typeOfT = new TypeToken<List<MessageBean>>() {
                                }.getType();
                                mb = new Gson().fromJson(ob, typeOfT);
                            }


                            mAdapter = new MessageAdapter(mb,getApplicationContext());
                            message_rec.setAdapter(mAdapter);

                        }else {

                            Toast.makeText(MessageListActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(MessageListActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
